<?php
class Front_IndexController extends Cff_Controller_FrontAction
{
	
	public function preDispatch()
	{
// 		$row = Front_Service_Posts::getByField("1713","ID");
// // 		print_r(htmlspecialchars($row['post_content']));

// 		$codebox = $this->_helper->getHelper('PostContentParser');
// 		$codebox->codebox($row['post_content']);
		
// 		exit;
// 		$content = $row['post_content'];
// 		preg_match_all("/\s*<pre(?:lang=[\"']([\w-]*)[\"']|file=[\"']([\w-]*\.?[\w-]*)[\"']|colla=[\"']([\+\-])[\"']|line=[\"'](\d*|n)[\"']|\s)+>(.*)<\/pre>\s*/siU", $content,$m);
		
// // 		print_r($m);
// 		exit;
	}
	
	public function indexAction()
	{
		$defaultPageSize = 20;
		$page = (int)$this->_request->getParam('page', 1);
		$pageSize = (int)$this->_request->getParam('pageSize', $defaultPageSize);
		$page = $page<1?1:$page;
		$pageSize = $pageSize<1?$defaultPageSize:$pageSize;

		$count = Front_Service_Posts::getByCondition(array(), "count(*)");
		$pageCount = $count%$pageSize==0?(int)($count/$pageSize):(int)($count/$pageSize+1);
		$page = $page>=$pageCount?$pageCount:$page;
		
		
// 		echo $page;exit;
		$result = Front_Service_Posts::getByCondition(array(), "*", "", $pageSize, $page);
		$codebox = $this->_helper->getHelper('PostContentParser');
		foreach($result as $k=>$v){
// 			$content = $v['post_content'];
// 			$content = htmlspecialchars($content);
// 			$content = explode("\n",$content);
// 			$tem = "";
// 			foreach($content as $con){
// 				$tem .= empty($con)?$con:"<p>{$con}</p>";
// 			}
// 			$content = $tem;
				

			$content = $codebox->contentParser($v['post_content']);
				
			$result[$k]['post_content'] = $content;
		}
		$this->view->count = $count;
		$this->view->perpage = $pageSize;
		$this->view->page = $page;
		$this->view->theurl = "/".$this->module."/".$this->controller."/".$this->action;
		$this->view->list = $result;
		// print_r($result);exit;
		echo Cff::renderPhtml("list.phtml"); // 视图文件为网页片段，使用layout
	}

}